/**
 * yccrmserver-core
 * @(#) BaseFrontServiceImpl.java 2011 2011-12-26 下午3:25:48
 * 
 * Copyright 2011 Chengdu Sunsharp Science & Techniques, Inc. All rights reserved.
 */
package com.huyanqi.core.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

import com.huyanqi.core.BaseDomainObject;
import com.huyanqi.core.dao.BaseDAO;
import com.huyanqi.core.service.BaseFrontService;

/**
 * <p>Title: BaseFrontServiceImpl.java</p>
 * 
 * <p>Description: 前端Service基类</p>
 * 
 * <p>Datetime: 2011-12-26 下午3:25:48</p>
 *
 * @company 成都映潮科技有限公司
 * 
 * @author <a href="mailto:liwei@sunsharp.cn">p.k.ripper</a>
 *
 */
@Transactional
public abstract class BaseFrontServiceImpl<T extends BaseDomainObject,PK extends Serializable,EntityDAO extends BaseDAO<T,PK>> implements BaseFrontService<T,PK>{
	protected Logger log = Logger.getLogger(this.getClass());

	protected EntityDAO entityDao;
	
	public EntityDAO getEntityDao() {
		return entityDao;
	}
	//由子类注入
	public abstract void setEntityDao(EntityDAO entityDao) ;
	@Override
	public T save(T entity) throws Exception {			
		return entityDao.saveEntity(entity);
	}

	@Override
	public void update(T entity) throws Exception {
		entityDao.updateEntity(entity);
	}

	@Override
	public void delete(T entity) throws Exception {
		
		entityDao.deleteEntity(entity);
	}

	@Override
	public void delete(PK[] ids) throws Exception {
		for(PK id:ids){
			if(null!=id)entityDao.deleteEntity(id);
		}
	}

	@Override
	public void delete(PK id) throws Exception {
		
		entityDao.deleteEntity(id);
	}

	@Override
	public T findById(PK id) throws Exception {	
		
		return entityDao.findEntityById(id);
	}

	@Override
	public List<T> findByIds(Collection<PK> ids) throws Exception {		
		
		return entityDao.findByIds(ids);
	}

	public List<T> findByProperty(String propertyName, Object value) throws Exception{		
		
		return entityDao.findByProperty(propertyName, value);
	}
	
	public List<T> findByProperty(Map<String, Object> values) throws Exception{
		
		return entityDao.findByProperty(values);
	}
	
	protected Criterion or(Criterion crit1, Criterion crit2) {
        if (crit1 == null) {
            return crit2;
        }
        if (crit2 == null) {
           return crit1;
        }
        return Restrictions.or(crit1, crit2);
    }
}